﻿package com.devilcat.ui{
	import flash.display.MovieClip;
	import flash.net.SharedObject;
	import flash.text.TextField;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	import flash.display.SimpleButton;    
	import flash.utils.getDefinitionByName;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	import com.devilcat.ui.FloatingText;
	import com.devilcat.ob.PVPPokemonob;
	import com.devilcat.dt.*;
	import com.devilcat.ws.PVPLoadPokemonws;
	import com.devilcat.ws.PlayerInforws;
	import com.devilcat.ws.UserPokemonws;


	public class PVPBattleui extends MovieClip {
		private var tweenList:Array;
		private var manualSwitch:Boolean=true;
		private var pokemonIsShowed:Boolean=false;
		private var floatSpace:Number=17;
		private var floatTime:Number=2;
		private var weather:String="None";
		private var weatherTurn:Number=-1;
		public  var autoSwitch:uint;
		private var TargetList:Array;
		public  var MyPokemon:PVPPokemonob;
		private var MyPokemonCurrentID:Number;
		public  var TargetPokemon:PVPPokemonob;
		public  var oldID:Number;
		public  var targetUID:Number;
		public 	var mws:PVPLoadPokemonws;
		public 	var tws:PVPLoadPokemonws;
		public  var mpkm:Pokemondt;
		public  var tpkm:Pokemondt;
		public  var itv:uint;
		public  var checkLeft:PlayerInforws;
		
		public function PVPBattleui(_TargetList:Array,_targetUID:Number) {
			TargetList=_TargetList;
			targetUID=_targetUID;			
			trace("Begin Battle List"+TargetList.length);
			for(var i:int=0;i<TargetList.length;i++){
				TargetList[i].uid=targetUID;
				trace(TargetList[i].uid +" "+TargetList[i].Name);
			}
			trace("End Battle "+TargetList.length);
			for(var j:int=0;j<Gamedt.Player.pokemonList.length;j++)
				Gamedt.Player.pokemonList[j].uid=targetUID;
			Gamedt.updatePlayer.Update("status",1);
			Gamedt.BattleUI=this;
			FreeTween();			
			tweenList.unshift( new Tween (this,"alpha",Elastic.easeOut,0,1,1,true));
			EnableMenu();
			
			var PokemonAlive:Number=CheckPokemonList(Gamedt.Player.pokemonList);
			var TargetPokemonAlive:Number=CheckPokemonList(TargetList);
			if(PokemonAlive<0){
				Gamedt.MainUI.AfterBattle("failed");
				this.parent.removeChild(this);
			}else{
				MyPokemonCurrentID=Gamedt.Player.pokemonList[PokemonAlive].upid;
				MyPokemon=new PVPPokemonob(Gamedt.Player.pokemonList[PokemonAlive]);
				AddTargetPokemon(TargetPokemonAlive);			
				AddMyPokemon();
			}	
			
//			MyPokemonCurrentID=Gamedt.Player.pokemonList[0].upid;
//			MyPokemon=new PVPPokemonob(Gamedt.Player.pokemonList[0]);
//			AddTargetPokemon(0);			
//			AddMyPokemon();
			oldID=TargetPokemon.pokemon.upid;
//			mpkm=Gamedt.Player.pokemonList[0];
//			tpkm=_TargetList[0];
			mws = new PVPLoadPokemonws(Gamedt.Player.uid,checkMyPokemon);
			tws = new PVPLoadPokemonws(targetUID,checkTargetPokemon);
			checkLeft=new PlayerInforws();
			itv = setInterval(checkLeftfc,200);
			
		}
		public function checkLeftfc(){
			checkLeft.GetInfor(targetUID,"pvpask",leftBattle);
		}
		public function leftBattle(ob:Object){
			if(String(ob.pvpask)=="0"){
				clearInterval(itv);
				RemoveBattle();				
			}			
		}
		public function checkMyPokemon(ob:Object){
			//if(ob.upid == MyPokemon.pokemon.upid){
//			trace("My pokemon : "+ob.Name);
//			trace("My CurrentHP : "+ob.CurrentHP);
//			trace("My upid : "+ob.upid + " " +String(ob.upid)==String(MyPokemon.pokemon.upid));
				MyPokemon.pokemon.CurrentHP=ob.CurrentHP;
				MyPokemon.ReText();
			//}
		}
		public function checkTargetPokemon(ob:Object){
		//	trace("Target pokemon : "+ob.Name);
			if(String(ob.upid) ==String(TargetPokemon.pokemon.upid)){
				TargetPokemon.pokemon.CurrentHP=ob.CurrentHP;
				TargetPokemon.ReText();
			}else{
				MyPokemon.ClearAuto();
				TargetPokemon.RemoveMeTween();				
				var getPos:uint;
				for(getPos=0;getPos<TargetList.length;getPos++){
					if(TargetList[getPos].upid==ob.upid){
						AddTargetPokemon(getPos);
						break;
					}
				}	
			}			
		}
		function render(newupid:int=0){
//			MyPokemon.ReText();
//			if(newupid>0){
//				MyPokemon.ClearAuto();
//				TargetPokemon.RemoveMeTween();				
//				var getPos:uint;
//				for(getPos=0;getPos<TargetList.length;getPos++){
//					if(TargetList[getPos].upid==newupid){
//						AddTargetPokemon(getPos);
//						break;
//					}
//				}		
//				
//			}
//			trace(MyPokemon.pokemon.Name+" MyPokemon "+MyPokemon.pokemon.CurrentHP +" "+ MyPokemon.pokemon.HP);
//			trace(mpkm.Name+" mpkm "+mpkm.CurrentHP +" "+ mpkm.HP);
//			trace(TargetPokemon.pokemon.Name+" TargetPokemon "+TargetPokemon.pokemon.CurrentHP +" "+ TargetPokemon.pokemon.HP);
//			trace(tpkm.Name+" tpkm "+tpkm.CurrentHP +" "+ tpkm.HP);
/*			MyPokemon.pokemon.CurrentHP = mpkm.CurrentHP
			MyPokemon.ReText();
			if(tpkm.upid==TargetPokemon.pokemon.upid){
				//TargetPokemon.pokemon.setPersonal(tpkm);
				TargetPokemon.pokemon.CurrentHP = tpkm.CurrentHP
				TargetPokemon.ReText();
			}else{
				MyPokemon.ClearAuto();
				TargetPokemon.RemoveMeTween();
				
				var getPos:uint;
				for(getPos=0;getPos<TargetList.length;getPos++){
					if(TargetList[getPos].upid==tpkm.upid){
						AddTargetPokemon(getPos);
						break;
					}
				}				
			}*/
			
		}
/*		function PVPAutoSwitchEnd(e:Event){
			var XMLData = new XML(e.target.data);
			//SQLData.mainClip.xmlDisplay.text=XMLData;
			var newLeadpkm:uint=Number(XMLData.leadpkm);
			if(newLeadpkm!=oldID && newLeadpkm!=0){
				oldID=newLeadpkm;
				MyPokemon.ClearAuto();
				TargetPokemon.RemoveMeTween();
				//TargetPokemon=null		
				var getPos:uint;
				for(getPos=0;getPos<TargetList.length;getPos++){
					if(TargetList[getPos].upid==newLeadpkm){
						AddTargetPokemon(getPos);
						break;
					}
				}
			}
		}*/
		function AddMyPokemon(){
			tweenList.unshift(new Tween (MyPokemon,"alpha",Regular.easeOut,0,1,1,true));
			MyPokemon.InitPokemon(true);
			MyPokemon.x=300;
			MyPokemon.setTarget(TargetPokemon);
			if(TargetPokemon)
			TargetPokemon.setTarget(MyPokemon);
			PokemonBattleMC.addChild(MyPokemon);	
		}
		function AddTargetPokemon(_targetID){			
			TargetPokemon=new PVPPokemonob(TargetList[_targetID]);
			tweenList.unshift(new Tween (TargetPokemon,"alpha",Regular.easeOut,0,1,1,true));
			TargetPokemon.InitPokemon(false);
			TargetPokemon.x=600;
			//TargetPokemon.setTarget(MyPokemon);
			TargetPokemon.AI();
			MyPokemon.setTarget(TargetPokemon);
			PokemonBattleMC.addChild(TargetPokemon);
		}
		function LoadPokemonList() {			
			while (PokemonPanel.numChildren > 0) {
				PokemonPanel.removeChildAt(0);
			}
			for (var i:int =0; i<Gamedt.Player.pokemonList.length; i++) {
				var pokemon:PokemonInfor = new PokemonInfor(Gamedt.Player.pokemonList[i]);
				pokemon.y=24*i;
				pokemon.AddEventSwitchPokemon();
				PokemonPanel.addChild(pokemon);
			}
		}
		/*
		
		private var myOldHP:Number=0;
		public function PVPBattleui(_TargetList:Array,_targetUID:String) {
			SQLData.battleClip=this;
			//TargetList=_TargetList;
			//targetUID=_targetUID;
			//FreeTween();			
			//tweenList.unshift( new Tween (this,"alpha",Elastic.easeOut,0,1,1,true));
			//EnableMenu();
			DelPokemonPanel.visible=false;
			//MyPokemonCurrentID=Gamedt.Player.pokemonList[0].upid;
			//MyPokemon=new PVPPokemonob(Gamedt.Player.pokemonList[0]);
			//AddTargetPokemon(0);			
			//AddMyPokemon();
			//oldID=TargetPokemon.pokemon.upid;
			autoSwitch= setInterval(PVPAutoSwitch,200);	
		}		
		*/
		private function FreeTween() {
			tweenList=null;
			tweenList=new Array();
		}
		//---------------------MENU
		private function EnableMenu() {
			ShowPokemon.addEventListener(MouseEvent.CLICK,ShowPokemonPanelEvent);
			OutBattle.addEventListener(MouseEvent.CLICK,Flee);
		}
		private function DisableMenu() {
			ShowPokemon.removeEventListener(MouseEvent.CLICK,ShowPokemonPanelEvent);
			OutBattle.removeEventListener(MouseEvent.CLICK,Flee);
		}
		private function HideAllPanel() {
			HidePokemonPanel();
		}
		private function ShowPokemonPanelEvent(e:MouseEvent) {
			manualSwitch=true;
			ShowPokemonPanel();
		}
		private function ShowPokemonPanel() {
			if (!pokemonIsShowed) {
				LoadPokemonList();
				HideAllPanel();				
				tweenList.unshift(new Tween (PokemonPanel,"y",Strong.easeOut,-272,-2,1,true));
				pokemonIsShowed=true;
				EnablePokemonSwitch();
			} else {
				HidePokemonPanel();
			}
		}
		private function HidePokemonPanel() {
			if (pokemonIsShowed) {				
				tweenList.unshift( new Tween (PokemonPanel,"y",Strong.easeOut,-2,-272,1,true));
				pokemonIsShowed=false;
				DisablePokemonSwitch();
			}
		}
		private function EnablePokemonSwitch() {
			PokemonPanel.addEventListener(MouseEvent.CLICK, ClickSwitchPokemon);
		}
		private function DisablePokemonSwitch() {
			PokemonPanel.removeEventListener(MouseEvent.CLICK, ClickSwitchPokemon);
		}
		private function ClickSwitchPokemon(evt:MouseEvent) {
			LoadPokemonList();
			if(MyPokemonCurrentID!=evt.target.pokemon.upid && evt.target.pokemon.CurrentHP>0){		
				MyPokemonCurrentID=evt.target.pokemon.upid;		
				if(MyPokemon.pokemon.CurrentHP>0){
					while (MyPokemon.numChildren > 0)
					{
						MyPokemon.removeChildAt(0);
					}
					TargetPokemon.ClearAuto();
					MyPokemon.RemoveMe();
					//PokemonBattleMC.removeChild(MyPokemon);
					MyPokemon=null;	
				}
				MyPokemon=new PVPPokemonob(evt.target.pokemon);
				AddMyPokemon();
				TargetPokemon.setTarget(MyPokemon);
				TargetPokemon.AI();
				HideAllPanel();
			}
		}
		public function SwitchTargetPokemon(){
			MyPokemon.ClearAuto();
			TargetPokemon.RemoveMe();
			TargetPokemon=null			
		}
		public function PokemonFailed(IsAI:Boolean){
			var PokemonAlive:Number;
			if(IsAI){
				MyPokemon.ClearAuto();
				PokemonAlive=CheckPokemonList(TargetList);
				if(PokemonAlive>0){
					//Cho` Auto Switch
//					TargetPokemon=new Pokemonob(TargetList[PokemonAlive]);
//					AddTargetPokemon();		
//					MyPokemon.setTarget(TargetPokemon);
				}else{				
					RemoveBattle();
				}
			}else{				
				TargetPokemon.ClearAuto();
				PokemonAlive=CheckPokemonList(Gamedt.Player.pokemonList);
				if(PokemonAlive>0){
					MyPokemon=new PVPPokemonob(Gamedt.Player.pokemonList[PokemonAlive]);
					AddMyPokemon();		
					TargetPokemon.setTarget(MyPokemon);
					//var Query:String="UPDATE twalk_user SET leadpkm='"+MyPokemon.pokemon.upid+"'  WHERE uid='"+SQLData.uid+"'";
					//SQLData.SendQuery(Query)
				}else{
					RemoveBattle();
				}
			}			
		}

		public function PVPAutoSwitch(){
			//SQLData.GetData("PVPSwitch",PVPAutoSwitchEnd,targetUID);		
		}

		private function RemoveBattle() {
			var lupl:UserPokemonws = new UserPokemonws(Gamedt.Player.uid);
			Gamedt.updatePlayer.Update("pvpask",0);
			Gamedt.updatePlayer.Update("pvpans",0);
			MyPokemon.RemoveMeTween();
			TargetPokemon.RemoveMeTween();
			tweenList.unshift(new Tween (this,"alpha",Regular.easeOut,1,0,1,true));
			tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,BackToGame);
		}
		private function BackToGame(e:Event) {					
			Gamedt.MainUI.AfterBattle("pvp");	
			if (this.parent != null) {
				this.parent.removeChild(this);
			}
		}
		private function Flee(e:Event) {
			DisableMenu();
			RemoveBattle();
		}
		public function CheckPokemonList(PokemonList:Array):Number {
			//return 1;
			trace("Check List :"+PokemonList.length);
			for (var i:int=0; i<PokemonList.length; i++) {
				trace("Pokemon :"+PokemonList[i].Name+", "+PokemonList[i].CurrentHP);
				if (PokemonList[i].CurrentHP>0) {
					return i;
				}
			}
			trace("End check List");
			return -1;
		}

	}
}